package xyz.csmcool.imchat.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.csmcool.imchat.service.RabbitmqService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 什锦
 * @Package xyz.csmcool.imchat.service.impl
 * @since 2022/8/14 0:38
 */

@Service
public class RabbitmqServiceImpl implements RabbitmqService {

    @Autowired
    FanoutSender fanoutSender;
    @Autowired
    PrivateChatFanoutSender privateChatFanoutSender;


    @Override
    public void sendMessage(Object message, Integer roomId)  {
        Map<String, Object> properties = Maps.newHashMapWithExpectedSize(2);
        String s = JSON.toJSONString(message);
        properties.put("roomId", roomId);
        //发送类型 1为聊天室信息
        properties.put("type",1);
        try {
            fanoutSender.send(s, properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * private*
     *
     * @param message
     * @param uid
     */
    @Override
    public void sendPrivateMessage(Object message, Integer uid,Integer senderId) {
        Map<String, Object> properties = Maps.newHashMapWithExpectedSize(2);
        String s = JSON.toJSONString(message);
        properties.put("uid", uid);
        properties.put("sid", senderId);
        //发送类型 1为聊天室信息
        properties.put("type",1);
        try {
            privateChatFanoutSender.send(s, properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
